package com.chuiyuan;

import com.chuiyuan.entity.Poem;
import com.chuiyuan.entity.Rule;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.List;
import java.util.Queue;

/**
 * Created by chuiyuan on 2/11/16.
 */
public class ProcessDoc {

    /**
     *
     * @param url
     * @param httpService
     * @return List<Poem></Poem>
     */
    public List<Poem> processGuShiWen(String url, HttpService httpService ){
        Rule rule = new Rule(url,
                null,null,
                null,
                -1,
                Rule.GET);
        Document doc = httpService.extrace(rule);
        if (doc == null){
            System.out.println("doc null");
            return null;
        }
        //left panel
        Elements mainEles = doc.select("div.leftlei");
        //String dynasty = mainEles.select("div.son1").first().text();
        Elements poemsEles = mainEles.select("div.son2");//size=7 category
        //System.out.println(poemsEles.size());

        List<Poem> poemList = new ArrayList<Poem>();
        //Queue<String> poemQueue =
        //Pattern p =Pattern.compile("\\(|\\)");

        //some times there is no author
        for (Element poemsEle : poemsEles){
            Elements poemEles = poemsEle.select("span");
            String category1 = poemEles.get(0).text(); //category with ":"
            String category = category1.substring(0,category1.length()-1);
            for (int i =1;i<poemEles.size();i++){
                Poem poem = new Poem() ;
                //poem.setDynasty(dynasty);
                poem.setCategory(category);
                poem.setHref(poemEles.get(i).select("a").attr("abs:href")+"/");//ref
                poem.setTitle(poemEles.get(i).select("a").text());
                poemList.add(poem);
            }
        }

        /*for (Poem poem : poemList){
            System.out.println(poem.getCategory()+
                    " "+poem.getTitle()+
                    " "+poem.getHref());
        }*/
        return poemList ;

    }

    /**
     * get details of poem
     * @param poem
     * @param httpService
     */
    public void processDetails(Poem poem, HttpService httpService ){
        String url = poem.getHref() ;
        //System.out.println(url);
        Rule rule = new Rule(url,
                null,null,
                null,
                -1,
                Rule.GET);
        Document doc = httpService.extrace(rule);
        if (doc == null) {
            System.out.println("doc=null");
            return;
        }

        Elements mainEles = doc.select("div.son2");
        //title already ok
        Elements poemDetailEles = mainEles.select("p");
        String dynasty = poemDetailEles.get(0).text().split("：")[1];//note，chinese :
        //Stng dynasty = poemDetailEles.get(0).getElementsByTag("span").text();
        //System.out.println(dynasty);
        String author = poemDetailEles.get(1).getElementsByTag("a").text();
        //System.out.println(author);
        String content = mainEles.text().split("原文：")[1];//note
        //System.out.println(content);
        //do not consider translation
        //Element translationEle = doc.select("#")

        poem.setDynasty(dynasty);
        poem.setAuthor(author);
        poem.setContent(content);

        //System.out.println(poem.toString());

    }
}
